﻿using System;
using System.Runtime.Serialization;
using System.Xml.Linq;
using NCMIS.ObjectModel.MetaData;

namespace NCMIS.ObjectModel
{
    /// <summary>
    /// Represents a CMIS object property of type <see cref="System.Decimal"/>.
    /// </summary>
    [DataContract(Name = "propertyDecimal", Namespace = CmisNs.Cmis)]
    public class CmisPropertyDecimal : CmisProperty
    {
        /// <summary>
        /// The data value of this instance.
        /// </summary>
        [DataMember(Name = "value")]
        public new decimal[] Value
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the single value from this instance. If multiple values exists, the first value is returned.
        /// If no value exists null is returned.
        /// </summary>
        [IgnoreDataMember]
        public Nullable<decimal> SingleValue
        {
            get
            {
                if (this.Value.Length > 0)
                    return this.Value[0];
                return null;
            }
        }

        /// <summary>
        /// Returns a <see cref="System.Xml.Linq.XElement"/> that represents the current instance.
        /// </summary>
        /// <returns>A <see cref="System.Xml.Linq.XElement"/> that represents the current instance.</returns>
        public override XElement ToXElement()
        {
            XNamespace ns = CmisNs.Cmis;
            XElement propertyElement = new XElement(ns + "propertyDecimal", new XAttribute("propertyDefinitionId", this.PropertyDefinitionId));
            if (this.Value != null)
            {
                propertyElement.Add(new XElement(ns + "value", this.Value));
                if (this.LocalName != null) propertyElement.SetAttributeValue("localName", this.LocalName);
                if (this.DisplayName != null) propertyElement.SetAttributeValue("displayName", this.DisplayName);
                if (this.QueryName != null) propertyElement.SetAttributeValue("queryName", this.QueryName);
            }

            return propertyElement;
        }
    }
}
